(function($) {
	var _lookup = {
		currentGroup : "",
		suffix : false,
		$target : null
	};
	var _util = {
		_lookupPrefix : function(key) {
			var strDot = _lookup.currentGroup ? "." : "";
			return _lookup.currentGroup + strDot + key;
		},
		lookupPk : function(key) {
			return this._lookupPrefix(key) + (_lookup.suffix ? "[]" : "");
		},
		lookupField : function(key) {
			return "cyanseed." + this.lookupPk(key);
		}
	};
	$.cyanseed = {
		bringBackSuggest : function(args) {
			var $box = _lookup['$target'].parents(".unitBox:first");
			$box.find(":input").each(function() {
				var $input = $(this), inputName = $input.attr("name");
				for ( var key in args) {
					var name = _util.lookupPk(key) || _util.lookupField(key);
					if (name == inputName) {
						$input.val(args[key]);
						break;
					}
				}
			});
		},
		autocomplete : function(item) {
			item.each(function() {
				var $input = $(this);
				var autocompleteOpts = DWZ.jsonEval($input
						.attr("autocompleteOpts"));
				var autocompleteValue = autocompleteOpts.autocomplteValue
						|| "name" || "label";
				var options = $input.metadata();
				var defaultOptions = {
					matchContains : true,
					dataType : "json",
					mustmatch : true,
					type : "POST",
					max : 20,
					selectFirst : true,
					parse : function(data) {
						return $.map(data, function(row) {
							return {
								data : row,
								value : row[autocompleteValue],
								result : row[autocompleteValue]
							};
						});
					},
					formatItem : function(row, i, max) {
						return row[autocompleteValue];
					},
					formatMatch : function(row, i, max) {
						return row[autocompleteValue];
					}
				};
				options = $.extend(defaultOptions, autocompleteOpts);
				var isUrl = typeof options.urlOrData == "string";
				$input.focus(function(event) {
					if (!isUrl) {
						return false;
					}
					var url = unescape(options.urlOrData).replaceTmById(
							$(event.target).parents(".unitBox:first"));
					if (!url.isFinishedTm()) {
						alertMsg.error($input.attr("warn")
								|| DWZ.msg("alertSelectMsg"));
						return false;
					}
					_lookup = $.extend(_lookup, {
						currentGroup : $input.attr("lookupGroup") || "",
						suffix : $input.attr("suffix") || false,
						$target : $input
					});
					options.url = url;
					$input.setOptions(options);
					$input.flushCache();
				});
				$input.autocomplete(options.urlOrData, options);
				$input.result(function(event, data, formatted) {
					var $this = $(this);
					var lookupAttrs = DWZ.jsonEval($this.attr("lookupAttrs"));
					for ( var key in lookupAttrs) {
						lookupAttrs[key] = data[lookupAttrs[key]];
					}
					$.cyanseed.bringBackSuggest(lookupAttrs);
				});
			});
		},
		ieformhack : function(item) {
			item.each(function() {
				$(this).submit(function() {
					if ($(this).attr("target") == 'callbackframe') {
						return true;
					} else {
						return false;
					}
				});
			});
		},
		mac : function(item) {
			item.each(function() {
				$(this).keyup(function(event) {
					for ( var code in DWZ.keyCode) {
						if (event.keyCode == DWZ.keyCode[code]) {
							return;
						}
					}
					$(this).val($(this).val().toUpperCase());
					$val = $(this).val().replaceAll(":", "");
					$valArr = [];
					for ( var i = 0; i < $val.length; i = i + 2) {
						$valArr.push($val.substr(i, 2));
					}
					$(this).val($valArr.join(":"));
					event.preventDefault();
				});
			});
		},
		progressbar : {
			options : {
				iCms : 1000,
				iMms : 60 * 1000,
				iHms : 3600 * 1000,
				iDms : 24 * 3600 * 1000
			},
			init : function() {
				var ops = {};
				$.cyanseed.progressbar.options = $.extend(ops,
						$.cyanseed.progressbar.options, {
							bg : $("#background"),
							pBg : $("#percentProgressBar")
						});
				ops.pBg.hide();
				ops.pBg.children(".pbar").progressbar({
					value : 0
				});
				ops.pBg.children(".percent").html("0%");
				ops.pBg.children(".elapsed").html("");
			},
			show : function() {
				var ops = $.extend($.cyanseed.progressbar.options, {
					start : new Date()
				});
				ops.vInterval = setInterval(
						function() {
							var iLeftMs = new Date() - ops.start;
							var iDays = parseInt(iLeftMs / ops.iDms);
							var iHours = parseInt((iLeftMs - (iDays * ops.iDms))
									/ ops.iHms);
							var iMin = parseInt((iLeftMs - (iDays * ops.iDms) - (iHours * ops.iHms))
									/ ops.iMms);
							var iSec = parseInt((iLeftMs - (iDays * ops.iDms)
									- (iMin * ops.iMms) - (iHours * ops.iHms))
									/ ops.iCms);
							ops.pBg.children(".elapsed").html(
									iDays + ' days ' + iHours + 'h:' + iMin
											+ 'm:' + iSec + 's');
						}, 1000);
				ops.pInterval = setInterval(
						$.cyanseed.progressbar.readprogress, 1000 * 5);
				ops.pBg.progressbar("option", "value", 0);
				ops.bg.show();
				ops.pBg.show("slow");
			},
			hide : function() {
				var ops = $.cyanseed.progressbar.options;
				clearInterval(ops.pInterval);
				clearInterval(ops.vInterval);
				ops.pBg.hide("slow");
				ops.bg.hide();
			},
			readprogress : function() {
				$
						.ajax({
							url : base + "/common/resource_progress.action",
							type : "POST",
							global : false,
							dataType : "json",
							timeout : 5 * 1000,
							cache : false,
							success : function(data) {
								var percentAge = Math
										.floor((parseInt(data.readedBytes) / parseInt(data.totalBytes)) * 100);
								$("#percentProgressBar").children(".pbar")
										.progressbar("option", "value",
												percentAge);
								$("#percentProgressBar").children(".percent")
										.html(percentAge + "%");
							}
						});
			}
		},
		downloadTodo : function(item) {
			function _doDownload(url) {
				window.location = url;
			}
			item.each(function(i) {
				var $this = $(this);
				$this.click(function(event) {
					var url = unescape($this.attr("href")).replaceTmById(
							$(event.target).parents(".unitBox:first"));
					DWZ.debug(url);
					if (!url.isFinishedTm()) {
						alertMsg.error($this.attr("warn")
								|| DWZ.msg("alertSelectMsg"));
						return false;
					}
					var title = $this.attr("title");
					if (title) {
						alertMsg.confirm(title, {
							okCall : function() {
								_doDownload(url);
							}
						});
					} else {
						_doDownload(url);
					}
					event.preventDefault();
				});
			});
		}
	};
	var xx = {
		"count" : 0,
		"startIndex" : 0,
		"status" : 2000,
		"response" : {
			"error" : "invalid_grant",
			"errorDescription" : "Invalid authorization code: ANXxSNjwQDugOnqeikRMu2bKaXCdlLxn"
		}
	}

})(jQuery);